<template>
  <div class="login-main">
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item label="账号" prop="uname">
        <el-input type="text" placeholder="请输入账号" v-model="form.uname"/>
      </el-form-item>
      <el-form-item label="密码" prop="upwd">
        <el-input type="password" placeholder="请输入密码" v-model="form.upwd"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-on:click="onSubmit('loginForm')" >登录</el-button>
        <el-button type="info" >重 置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    name: "Login",
    data() {
      return {
        form: {
          uname: '',
          upwd: ''
        },
        // dialogVisible:false,
        // 表单验证，需要在 el-form-item 元素中增加 prop 属性
        rules: {
          uname: [
            {required: true, message: '账号不可为空', trigger: 'blur'}
          ],
          upwd: [
            {required: true, message: '密码不可为空', trigger: 'blur'}
          ]
        },
      }
    },
    methods: {
      onSubmit(formName) {
        // 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // this.$router.push('/home');
            this.$axios.get("/userMsg?loginName="+this.form.uname).then((res)=>{
              // console.log(res.data.data);
              let userMsg = res.data.data;
              if(userMsg.length > 0) {
                this.$store.commit('getUser',userMsg[0]);
                if (userMsg[0].password==this.form.upwd){
                  //密码正确，跳转页面
                  this.$router.push('/Home');
                }else{
                  //弹出警告框,密码错误
                  this.$message({
                  message: '密码错误，请重新输入密码',
                  type: 'warning'});
                }
              }else{
                  //弹出警告框,用户名不存在
                  this.$message({
                  message: '用户名不存在，请先注册用户',
                  type: 'warning'});
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="less" scoped>
  .login-main{
    background-image: url('../assets/banner.jpg');
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    height: 100%;
  }
  .login-box {
    border: 1px solid #DCDFE6;
    width: 350px;
    position: absolute;
    left: 50%;
    top: 50%;
    padding: 25px 35px 1px 35px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    box-shadow: 0 0 25px #909399;
    transform: translate(-50%, -50%);
    background-color: white;
  }

  .login-title {
    text-align: center;
    margin: 0 auto 40px auto;
    color: #303133;
  }
</style>
